<template>
  <el-card class="box-card card-main" shadow="never">
    <div slot="header">
      <el-form
        :inline="true"
        :model="serchData"
        class="demo-form-inline"
        label-width="90px"
        label-position="right"
        size="mini"
      >
        <el-form-item label="客户端名称：">
          <el-input v-model="serchData.name" style="width:150px" placeholder="请输入客户端名称" />
        </el-form-item>
        <el-form-item label="项目名称：">
          <el-input v-model="serchData.projectName" style="width:150px" placeholder="请选择项目名称" />
        </el-form-item>
        <el-form-item label="客户端IP：">
          <el-input v-model="serchData.clientIp" style="width:150px" placeholder="请输入客户端IP" />
        </el-form-item>
        <el-form-item label="客户端状态：">
          <el-select v-model="serchData.status" style="width:150px" placeholder="请选择客户端状态">
            <el-option label="请选择" value />
            <el-option label="正常" :value="1" />
            <el-option label="连接失败" :value="2" />
            <el-option label="状态未知" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchList">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row>
      <el-button size="mini" @click="addRow()">新增客户端</el-button>
    </el-row>
    <el-table v-loading="loading" :data="dataList" border stripe size="small" style="width: auto">
      <el-table-column type="selection" align="left" width="35" />
      <el-table-column width="150" align="left" label="客户端名称">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column width="100" align="left" label="项目名称">
        <template slot-scope="scope">{{ scope.row.projectName }}</template>
      </el-table-column>
      <el-table-column align="left" label="客户端IP">
        <template slot-scope="scope">{{ scope.row.clientIp }}</template>
      </el-table-column>
      <el-table-column align="left" label="客户端端口">
        <template slot-scope="scope">{{ scope.row.clientPort }}</template>
      </el-table-column>
      <el-table-column width="100" align="left" label="客户端状态">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? "正常" :"" }}
          {{ scope.row.status === 2 ? "连接失败" :"" }}
          {{ scope.row.status === 3 ? "状态未知" :"" }}
        </template>
      </el-table-column>
      <el-table-column align="left" label="备注">
        <template slot-scope="scope">{{ scope.row.remark }}</template>
      </el-table-column>
      <el-table-column width="140" align="left" label="创建时间">
        <template slot-scope="scope">{{ scope.row.createTime }}</template>
      </el-table-column>
      <el-table-column width="180" fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            style="padding: 3px 4px 3px 4px;margin: 1px"
            size="mini"
            @click="addRow(scope.row)"
          >修改</el-button>
          <el-button
            type="danger"
            style="padding: 3px 4px 3px 4px;margin: 1px"
            size="mini"
            @click="deleteRow(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="pageInfo.pageNum"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.pageCount"
      @size-change="changePageSize"
      @current-change="changePageNum"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrEditFormVisible" ref="addOrUpdate" @refreshDataList="getPageList" />
  </el-card>
</template>

<script>
import { getClientsPage, delClientsById } from '@/api/publicConfig/client'
import AddOrUpdate from './client-add-or-update'
export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      loading: false,
      serchData: { name: '', projectName: '', clientIp: '', status: '' },
      pageInfo: { pageNum: 1, pageSize: 10, pageCount: 0 },
      dataList: [],
      addOrEditFormVisible: false // 新增或者删除弹出框默认值
    }
  },
  computed: {
    serchInfo: function() {
      const pageInfo = this.pageInfo
      const serchData = this.serchData
      pageInfo.serchData = serchData
      return pageInfo
    }
  },
  mounted() {
    this.getPageList()
  },
  methods: {
    resetSearch() {
      // 重置搜索条件方法
      this.serchData = { name: '', projectName: '', clientIp: '', status: '' }
    },
    searchList() {
      this.getPageList()
    },
    changePageSize(row) {
      this.pageInfo.pageSize = row
      this.getPageList()
    },
    changePageNum(row) {
      this.pageInfo.pageNum = row
      this.getPageList()
    },
    addRow(row) {
      this.addOrEditFormVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(row)
      })
    },
    deleteRow(row) {
      this.$confirm('此操作将永久删除该客户端, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delClientsById(row).then(data => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getPageList()
        })
      }).catch(() => {
        this.$message({
          type: 'success',
          message: '已取消删除'
        })
      })
    },
    // 获取分页客户端列表
    getPageList() {
      this.loading = true
      getClientsPage(this.serchInfo).then((result) => {
        this.dataList = result.list || []
        this.pageInfo.pageCount = result.total
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>
</style>
